<?php


require_once '../schema/SchemaLoader.php';
SchemaLoader::getInstance();
 

$xml = simplexml_load_file("./old.xml");

print "<pre>";

$schema = new Schema();
foreach( $xml->table as $xtable ){
	print $xtable["name"]."\n";
	$table = new Table();
	$table->setTableName("".$xtable["name"]);
	
	$tmp = explode("|", $xtable["description"]."");
	$table->setBaseClasspath($tmp[0]);
	$table->setClasspath($tmp[1]);
	
	
	foreach($xtable->column as $xfield){
		$field = new Field();
		$field->setName($xfield["name"]."");
		$field->setType($xfield["type"]."");
		$field->setSize($xfield["size"]."");
		$field->setAutoincrement($xfield["autoincrement"]."".""=="true"?true:false);
		$field->setDefault($xfield["default"].""=="true"?true:false);
		$field->setRequired($xfield["required"]."".""=="true"?true:false);
		$field->setPKey($xfield["primaryKey"]."".""=="true"?true:false);
		
		
		$table->addField($field);
		
	}
	
	foreach($xtable->index as $xindex){
		$index = new Index();
		$index->setName($xindex["name"]."");
		foreach($xindex->children() as $column){
			$index->addFieldName($column["name"]."");
		}
		$table->addIndex($index);
	}
	
	foreach( $xtable->children() as $tag => $_node ){
		if($tag == 'foreign-key'){
			$fk = new ForeignKey();
			$fk->setName($_node["name"]."");
			$fk->setForeignTableName($_node["foreignTable"]."");
			foreach($_node->children() as $column){
				$fKeyReferece = new ForeignKeyReference();
				$fKeyReferece->setLocalFieldName($column["local"]."");
				$fKeyReferece->setForeignFieldName($column["foreign"]."");
				$fk->addReference($fKeyReferece);
			}
			$table->addForeignKey($fk);
		}
	}
	
	/*
	<foreign-key foreignTable="cms_pages" onUpdate="cascade" onDelete="cascade">
      <reference local="page_id" foreign="id"></reference>
    </foreign-key>
    <index name="f_key_index_cms_page_place_conf_page_id">
      <index-column name="page_id"></index-column>
    </index>
	*/
	
	$schema->addTable($table);
}

$writer = new SchemaWriter();

$writer->writeSchemaToFile($schema, "./new.xml");


?>